package org.hengguan.api.contest.module.contest.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hengguan.api.contest.module.contest.service.SignInService;
import org.hengguan.api.contest.module.contest.vo.GroupVo;
import org.hengguan.api.contest.module.contest.vo.RecordVo;
import org.hengguan.api.contest.module.contest.vo.SignInVo;
import org.hengguan.api.contest.vo.ResponseVo;
import org.hengguan.base.contest.model.contest.PlayerRecordModel;
import org.hengguan.base.contest.model.contest.SignGroupModel;
import org.hengguan.base.contest.service.contest.ParamConfigService;
import org.hengguan.base.contest.service.contest.PlayerRecordService;
import org.hengguan.base.contest.service.contest.SignGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"签到分组"})
@RestController
@RequestMapping("/sign")
public class SignInController {

    @Autowired
    private PlayerRecordService playerRecordService;

    @Autowired
    private SignGroupService signGroupService;

    @Autowired
    ParamConfigService paramConfigService;

    @Autowired
    SignInService signInService;

    @ApiOperationSupport(includeParameters = {
            "plId",
            "isSeed"
    })
    @ApiOperation(value = "注册",notes = "注册")
    @PostMapping("/regist")
    public ResponseVo<SignInVo> regist(SignInVo signInVo){

        SignGroupModel signGroupModel = signGroupService.getOne(new QueryWrapper<SignGroupModel>()
                .eq("pl_id", signInVo.getPlId())
                .last("LIMIT 1")
        );
        if (signGroupModel == null) {

            PlayerRecordModel playerRecordModel = playerRecordService.getById(signInVo.getPlId());
            if (playerRecordModel == null) {
                return ResponseVo.error("选手没有入档，无法报名");
            }
            signGroupModel = SignGroupModel.builder()
                    .plId(playerRecordModel.getId())
                    .plName(playerRecordModel.getPlName())
                    .plTelephone(playerRecordModel.getPlTelephone())
                    .plSex(playerRecordModel.getPlSex())
                    .plAddress(playerRecordModel.getPlAddress())
                    .isSeed(signInVo.getIsSeed())
                    .build();
            signGroupModel.insert();
            return ResponseVo.ok(signInVo.setPlName(signGroupModel.getPlName())
                    .setPlTelephone(signGroupModel.getPlTelephone())
                    .setPlSex(signGroupModel.getPlSex())
                    .setPlAddress(signGroupModel.getPlAddress())
            );
        }else {
            return ResponseVo.error(signGroupModel.getPlName()+" 已报名");
        }
    }

    @ApiOperation(value = "分组",notes = "分组")
    @GetMapping("/grouping")
    public ResponseVo<String> grouping(Integer plSize){

        List<SignGroupModel> signGroupModelList = signGroupService.list(new QueryWrapper<SignGroupModel>()
                .eq("is_seed", 1)
        );
        signGroupModelList.forEach(signGroupModel -> {
            signInService.groupLeader(signGroupModel);
        });
        for (Integer i = 0; i < plSize; i++) {
            signInService.randomGrouping();
        }

        return ResponseVo.ok("抽签分组完成");
    }
}
